package com.deyuanyun.pic.ctrl.service.impl;

import com.deyuanyun.pic.ctrl.mapper.ImportMapper;
import com.deyuanyun.pic.ctrl.service.ImportService;
import com.deyuanyun.pic.ctrl.util.AutoImport;
import com.deyuanyun.pic.ctrl.util.AutoImportInterface;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.service.msgtemplate.PushMessage;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.zip.GZIPOutputStream;

/**
 * 导入excel
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/19）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
@Service
public class ImportServiceImpl implements ImportService {
    @Resource
    private ImportMapper importMapper;

    @Override
    @PushMessage({"#data", "#c"})
    public boolean saveImportData(List<? extends AutoImportInterface> data, Class c) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ParseException {
        String exInspTaskId=AutoImport.getExInspTaskId();
        String table=AutoImport.getClassMappingTable(c);
        String insertSql=AutoImport.getClassToSql(c);
        String values=null;

        //更新原始数据状态为历史数据
        if(table != null)
        {
            importMapper.updataTableStatus(table,exInspTaskId);
        }

        //自动注入id，当前登录人，时间
        AutoImport.autoFiled(data);


        for(Object obj:data)
        {

            values=AutoImport.geObjectFiledValue(obj);
            importMapper.insertData(insertSql,values);

        }

        //关联类添加
        List mappTabl=AutoImport.getMappTabl(data);
        if(mappTabl.size() > 0 )
        {
            AutoImport.autoFiled(mappTabl);
            insertSql=AutoImport.getClassToSql(mappTabl.get(0).getClass());
            for(Object obj:mappTabl)
            {
                values=AutoImport.geObjectFiledValue(obj);
                importMapper.insertData(insertSql,values);
            }
        }

        //更新检测任务状态
        importMapper.updataExInspTaskStatus(exInspTaskId);

        return true;
    }

    @Override
    public HashMap<String, Object> queryNagm(String pipeLineId,String mileage) {
        return importMapper.queryNagm(pipeLineId,mileage);
    }

    @Override
    public HashMap<String, Object> queryOclpointByCustomNumber(String pipeLineId,String customNumber) {
        String exInspTaskId=AutoImport.getExInspTaskId();
        return importMapper.queryOclpointByCustomNumber(customNumber,exInspTaskId);
    }
    public Long queryOclpointByCustomNumberCount(String customNumber)
    {
        String exInspTaskId=AutoImport.getExInspTaskId();
        return importMapper.queryOclpointByCustomNumberCount(customNumber,exInspTaskId);
    }

    @Override
    public void inserMiorData(HashMap dataMap) throws IOException {

        /**
         * gzip 压缩
         */
        String dataStr= (String) dataMap.get("data");
        dataStr="{\"bool\": true, \"message\": \"success\", \"needFormat_\": \"need\", \"result\": " + dataStr + "}";


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(baos);
        byte data[] =dataStr.getBytes();
        gos.write(data, 0, data.length);
        gos.finish();
        gos.flush();
        gos.close();
        dataMap.put("data",baos.toByteArray());

        UserSessionVO sessionVO= SessionCacheSupport.getSessionVO();
        dataMap.put("optUser",sessionVO.getUsername());

        //删除原来的数据
        importMapper.deleteMiorData(dataMap);

        //导入新的数据
        importMapper.inserMiorData(dataMap);

        if("ac".equals(dataMap.get("type")))
        {
            //更新平均值
            importMapper.updataAcOrDcData(dataMap);
        }else{
            importMapper.updataDcData(dataMap);

        }



    }

    public Double queryAcSoilResistivity(String baseId)
    {
        return importMapper.queryAcSoilResistivity(baseId);
    }

    public String  queryTestStationId(String type,String baseId)
    {

        return importMapper.queryTestStationId(type,baseId);
    }

    public String queryAcOrDc(String type,String baseId)
    {
        return importMapper.queryAcOrDc(type,baseId);
    }


    public String queryInspLineId(String taskId)
    {
        return importMapper.queryInspLineId(taskId);
    }
}
